package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.PersonalSettingsService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.basic.BaseSettingModifyRequest;
import com.ktwlsoft.archivemanage.payload.basic.ResetPasswordRequest;
import com.ktwlsoft.archivemanage.payload.basic.RetrievePasswordByPhoneRequest;
import com.ktwlsoft.archivemanage.payload.basic.VerificationCodeRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 个人设置
 *
 * @author zhuhe on 2019/10/25 10:24
 * @version 1.0
 */
@Api(value = "个人设置",description = "个人设置")
@RestController
@RequestMapping("personalSettings")
public class PersonalSettingsController {

    /**个人设置*/
    private final PersonalSettingsService personalSettingsService;

    public PersonalSettingsController(PersonalSettingsService personalSettingsService) {
        this.personalSettingsService = personalSettingsService;
    }

    /**
      * 基本设置详情 on 2019/10/25 10:43
      *
      * @param id id
      * @return ResponseEntity<?>
      */
    @ApiOperation(value = "基本设置详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("baseSettingDetail")
    @UserAuthenticate(actionCode = "SELF_DETAIL", skip = true)
    public ResponseEntity<?> baseSettingDetail(@ApiParam(name = "id", value = "账号id") @RequestParam(defaultValue = "", required = true) Long id){
        return ResponseEntity.ok(personalSettingsService.baseSettingDetail(id));
    }

    /**
     * 基本设置修改 on 2019/10/25 10:43
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "基本设置修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("baseSettingModify")
    @UserAuthenticate(actionCode = "SELF_INFOR")
    public ResponseEntity<?> baseSettingModify(@Valid BaseSettingModifyRequest request) {
        return ResponseEntity.ok(personalSettingsService.baseSettingModify(request));
    }

    /**
     * 修改密码 on 2019/10/24 17:02
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "修改密码", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "modifyPassword")
    @UserAuthenticate(actionCode = "SELF_PASSWORDS")
    public ResponseEntity<?> modifyPassword(@Valid @RequestBody ResetPasswordRequest request,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseEntity.ok(Result.body(request, ResultType.CHECK_ERROR).withMessage(bindingResult.getFieldError().getDefaultMessage()));
        }
        return ResponseEntity.ok(personalSettingsService.modifyPassword(request));
    }


    /**
     * 修改手机号 on 2019/5/17 11:25
     *
     * @return ResponseEntity<Object>
     */
    @ApiOperation(value = "修改手机号", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "updatePhone")
    @UserAuthenticate(actionCode = "SELF_PHONE")
    public ResponseEntity<Object> updatePhone(@Valid @RequestBody VerificationCodeRequest codePojo) {
        return ResponseEntity.ok(personalSettingsService.updatePhone(codePojo));
    }

    /**
     * 修改邮箱 on 2019/5/17 11:25
     *
     * @return ResponseEntity<Object>
     */
    @ApiOperation(value = "修改邮箱", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "updateMail")
    @UserAuthenticate(actionCode = "SELF_EMAIL")
    public ResponseEntity<Object> updateMail(@Valid @RequestBody VerificationCodeRequest codePojo) {
        return ResponseEntity.ok(personalSettingsService.updateMail(codePojo));
    }

    /**
     * 通过手机号找回密码 on 2019/10/24 17:02
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "通过手机号找回密码", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "retrievePasswordByPhone")
    public ResponseEntity<?> retrievePasswordByPhone(@Valid @RequestBody RetrievePasswordByPhoneRequest request) {
        return ResponseEntity.ok(personalSettingsService.retrievePasswordByPhone(request,2));
    }

    /**
     * 微信通过手机号找回密码 on 2019/10/24 17:02
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "微信通过手机号找回密码", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "wxRetrievePasswordByPhone")
    public ResponseEntity<?> wxRetrievePasswordByPhone(@Valid @RequestBody RetrievePasswordByPhoneRequest request) {
        return ResponseEntity.ok(personalSettingsService.retrievePasswordByPhone(request,1));
    }

    /**
     * 微信修改密码 on 2019/10/24 17:02
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "微信修改密码", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "wxModifyPassword")
    public ResponseEntity<?> wxModifyPassword(@Valid @RequestBody ResetPasswordRequest request,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseEntity.ok(Result.body(request, ResultType.CHECK_ERROR).withMessage(bindingResult.getFieldError().getDefaultMessage()));
        }
        return ResponseEntity.ok(personalSettingsService.modifyPassword(request));
    }
}